package cn.edu.nju.software.action.admin;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.AdminShow;
import cn.edu.nju.software.bean.storage.Admin;
import cn.edu.nju.software.service.intf.AdminService;
import cn.edu.nju.software.util.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AdminLoginAction extends ActionSupport implements SessionAware, ServletResponseAware {
	
	private String signInName;
	private String password;
	private AdminService adminService;
	private Map<String, Object> session;
	private HttpServletResponse response;

	public void setSignInName(String signInName) {
		this.signInName = signInName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		response.setCharacterEncoding("UTF-8");
	}
	
	public void login() throws Exception {
		Admin admin = adminService.login(signInName, password);
		if (admin != null) {
			session.put(Constants.SESSION_ADMIN, new AdminShow(admin));
			response.getWriter().write(SUCCESS);
		}else {
			response.getWriter().write(ERROR);
		}
	}

}
